//
//  WYCShoukuanViewController.m
//  51信用卡
//
//  Created by 王晓民 on 2018/2/27.
//  Copyright © 2018年 中企互联. All rights reserved.
//

#import "WYCShoukuanViewController.h"
#import "WYCAddPayViewController.h"
#import "WYCdeleteViewController.h"
@interface WYCShoukuanViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,strong) UIImageView  *zhifubao;
@property(nonatomic,copy)NSString *imageName;
@property(nonatomic,strong) UILabel  *zfLb1;
@property(nonatomic,strong) UILabel  *wxLb1 ;
@property(nonatomic,strong) NSMutableArray  *nameArr ;
@property(nonatomic,strong) NSMutableArray  *userIdArr ;
@property(nonatomic,strong) NSMutableArray  *imgArr ;
@property(nonatomic,strong)UILabel  *bankLb;
@property(nonatomic,strong)UICollectionView * mainCollectionView;
@property(nonatomic,strong) UIView *bgView;

@end

@implementation WYCShoukuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = WYCGlobalBg;
    _nameArr = [[NSMutableArray alloc]init];
    _userIdArr = [[NSMutableArray alloc]init];
    _imgArr = [[NSMutableArray alloc]init];

    [self requster];

}
-(void)setView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 5);
    layout.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 5);
    
    //该方法也可以设置itemSize
    layout.itemSize =CGSizeMake(ScreenWidth/2-10, 200);
    
    _mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, NavHeight+10, ScreenWidth-10, ScreenHeight) collectionViewLayout:layout];
    [self.view addSubview:_mainCollectionView];
    _mainCollectionView.backgroundColor = WYCGlobalBg;
    //3.注册collectionViewCell
    //注意，此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致 均为 cellId
    [_mainCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
    
    //注册headerView  此处的ReuseIdentifier 必须和 cellForItemAtIndexPath 方法中 一致  均为reusableView
    [_mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"reusableView"];
    
    //4.设置代理
    _mainCollectionView.delegate = self;
    _mainCollectionView.dataSource = self;
}
-(void)pressAdd
{
    WYCAddPayViewController *addVuew = [[WYCAddPayViewController alloc]init];
    [self.navigationController pushViewController:addVuew animated:YES];
}

- (void)requster{
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"token"] = [WYCAccountTool unarchivetoken];
    NSLog(@"%@",params);
    
    [HttpTool POST:findCode params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"收款码%@",responseObject);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSArray * resultArray = responseObject[@"result"];
            for (NSDictionary * dict in resultArray) {
                NSString  *url = dict[@"url"];
                NSString  *userId = dict[@"id"];
                NSString  *name = dict[@"name"];
                [_imgArr addObject:url];
                [_userIdArr addObject:userId];
                [_nameArr addObject:name];
            }

            NSLog(@"%@    %@",_imgArr,_nameArr);
            [self setView];

        }else{
            
            [self showToast:msgStr];
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [self showToast:Failed];
    }];
    
}
//设置分区数（必须实现）
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//设置每个分区的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _nameArr.count+1;
}

//设置返回每个item的属性必须实现）
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    [_nameArr addObject:@"添加二维码"];
    [_imgArr addObject:@"last"];
    UICollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    
    UIImageView  *bankBg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth/2-20, 175)];
    bankBg.userInteractionEnabled = YES;
   [bankBg sd_setImageWithURL:[NSURL URLWithString:[_imgArr objectAtIndex:indexPath.row]] placeholderImage:[UIImage imageNamed:@"erweima"]];
    bankBg.contentMode =UIViewContentModeScaleAspectFit;

    
    [cell.contentView addSubview:bankBg];
    
    _bankLb = [[UILabel alloc]initWithFrame:CGRectMake(0, bankBg.frame.size.height+bankBg.frame.origin.y+5, ScreenWidth/2-10, 30 )];
    _bankLb.text = [_nameArr objectAtIndex:indexPath.row];
    _bankLb.textColor = [UIColor grayColor];
    _bankLb.textAlignment =NSTextAlignmentCenter;
    _bankLb.font = [UIFont systemFontOfSize:13];
    [cell.contentView addSubview:_bankLb];
    
    return cell;
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row==_userIdArr.count) {
        [self pressAdd];
    }
    else{
        [self tapAction:indexPath.row];
    }
}

- (void) tapAction:(NSInteger)index
{
    //创建一个黑色背景
    //初始化一个用来当做背景的View。我这里为了省时间计算，宽高直接用的5s的尺寸
    _bgView= [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [_bgView setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:_bgView];
    
    //创建显示图像的视图
    //初始化要显示的图片内容的imageView（这里位置继续偷懒...没有计算）
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    //要显示的图片，即要放大的图片
    imgView.contentMode =UIViewContentModeScaleAspectFit;
    [imgView sd_setImageWithURL:[NSURL URLWithString:[_imgArr objectAtIndex:index]]];

    [_bgView addSubview:imgView];
    
    imgView.userInteractionEnabled = YES;
    //添加点击手势（即点击图片后退出全屏）
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeView)];
    [imgView addGestureRecognizer:tapGesture];
    
}
-(void)closeView{
    [_bgView removeFromSuperview];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    CALayer *bottomLineLayer = [[CALayer alloc] init];
    bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    bottomLineLayer.backgroundColor = WYCmainColor.CGColor;
    [NavView.layer addSublayer:bottomLineLayer];
    //返回按钮
    UIButton *  fanhuibutton = [[UIButton alloc] init];
    [fanhuibutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [fanhuibutton addTarget:self action:@selector(photoClick) forControlEvents:UIControlEventTouchUpInside];
    fanhuibutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:fanhuibutton];
    
    
    //返回按钮
    UIButton *  editbutton = [[UIButton alloc] init];
    [editbutton setTitle:@"编辑" forState:UIControlStateNormal];
    [editbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [editbutton addTarget:self action:@selector(presEdit) forControlEvents:UIControlEventTouchUpInside];
    editbutton.frame = CGRectMake(ScreenWidth-60, titleY, 40 , 20);
    [NavView addSubview:editbutton];
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"收款码";
    [NavView addSubview:NavRigsterLabel];

}
-(void)presEdit
{
    WYCdeleteViewController  *delete = [[WYCdeleteViewController alloc]init];
    [self.navigationController pushViewController:delete animated:YES];
}
- (void)photoClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}


@end
